package com.yuesystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuesystem.common.CustomException;
import com.yuesystem.common.R;
import com.yuesystem.entity.AcademyItem;
import com.yuesystem.entity.ChangeItem;
import com.yuesystem.mapper.ChangeItemMapper;
import com.yuesystem.service.AcademyItemService;
import com.yuesystem.service.AcademyService;
import com.yuesystem.service.ChangeItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Service
@Slf4j
public class ChangeItemServiceImpl extends ServiceImpl<ChangeItemMapper, ChangeItem> implements ChangeItemService {
    @Autowired
    private AcademyItemService academyItemService;

    /**
     * 增加条目信息，保存在academy_item表和change_item表中
     *
     * @param academyItem
     * @param id
     */
    @Override
    @Transactional
    public void addItem(AcademyItem academyItem, Long id) {
        if (academyItem.getObject() == null || academyItem.getSubject() == null || academyItem.getRelation() == null) {
            throw new CustomException("新增条目不能为空！");
        }

        //查询数据库中有没有重复的该书院的条目
        LambdaQueryWrapper<AcademyItem> query = new LambdaQueryWrapper<>();
        query.eq(AcademyItem::getObject, academyItem.getObject())
                .eq(AcademyItem::getSubject, academyItem.getSubject())
                .eq(AcademyItem::getRelation, academyItem.getRelation())
                .eq(AcademyItem::getAcademyId, academyItem.getAcademyId());
        AcademyItem one = academyItemService.getOne(query);
        if (one != null) {
            throw new CustomException("该条目已存在，请重新输入！");
        } else {
            //将修改的信息条目加入到academy_item表中
            academyItem.setCreateUser(id);
            academyItem.setUpdateUser(id);
            academyItem.setUpdateTime(LocalDateTime.now());
            academyItemService.save(academyItem);
        }

        //获得该增加条目的id
        LambdaQueryWrapper<AcademyItem> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AcademyItem::getSubject, academyItem.getSubject())
                .eq(AcademyItem::getObject, academyItem.getObject())
                .eq(AcademyItem::getRelation, academyItem.getRelation())
                .eq(AcademyItem::getAcademyId, academyItem.getAcademyId());
        AcademyItem item = academyItemService.getOne(queryWrapper);

        log.info("增加条目信息:{}", item);

        ChangeItem changeItem = new ChangeItem();
        changeItem.setItemId(item.getId());
        changeItem.setSubjectNew(academyItem.getSubject());
        changeItem.setObjectNew(academyItem.getObject());
        changeItem.setRelationNew(academyItem.getRelation());
        changeItem.setUserId(id);
        changeItem.setOperate("增加");

        log.info("增加操作信息 :{}", changeItem);

        this.save(changeItem);
    }

    /**
     * 删除academy_item表中该条目的信息，保存用户删除信息在change_item表中
     * @param academyitem
     * @param userId
     */
    @Override
    @Transactional
    public void deleteItem(AcademyItem academyitem, Long userId) {
        //从academy_item表中删除该条目的信息
        academyItemService.removeById(academyitem);


    }
}
